import datetime

from django.contrib.auth.admin import UserAdmin
from pytz import timezone
from rest_framework.serializers import Field

from mirror_mirror_backend.settings import TIME_ZONE


class TimestampField(Field):

    def to_internal_value(self, data):
        # 前端时间修正
        timestamp = float(data)/1000
        no_tz = datetime.datetime.utcfromtimestamp(timestamp)
        no_tz += datetime.timedelta(hours=8)    #
        return no_tz.astimezone(timezone(TIME_ZONE))

    def to_representation(self, value):
        return value.timestamp()*1000


class UserProfileAdmin(UserAdmin):
    list_display = ('username', 'nickname', 'is_superuser', 'is_staff', 'is_active', 'date_joined')
    fieldsets = (
        (None, {'fields': ('username', 'password', 'nickname','photo')}),
    )